﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace StudentManagementSystem
{
    public partial class Form2 : Form
    {
        private Database db = new Database();
        public Form2()
        {
            InitializeComponent();
            LoadClasses();
        }

        // 加载班级信息
        private void LoadClasses()
        {
            using (var conn = db.GetConnection())
            {
                string query = "SELECT * FROM Class";
                MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
                DataTable table = new DataTable();
                adapter.Fill(table);
                dataGridView1.DataSource = table;
            }
        }

        // 添加班级
        private void AddClass(string className, int schoolID)
        {
            using (var conn = db.GetConnection())
            {
                string query = "INSERT INTO Class (ClassName, SchoolID) VALUES (@className, @schoolID)";
                MySqlCommand command = new MySqlCommand(query, conn);
                command.Parameters.AddWithValue("@className", className);
                command.Parameters.AddWithValue("@schoolID", schoolID);

                conn.Open();
                command.ExecuteNonQuery();
                MessageBox.Show("班级添加成功！");
                LogAction($"成功添加班级：{className}");
                LoadClasses();
            }
        }

        // 删除班级
        private void DeleteClass(int classID)
        {
            using (var conn = db.GetConnection())
            {
                string query = "DELETE FROM Class WHERE ClassID = @classID";
                MySqlCommand command = new MySqlCommand(query, conn);
                command.Parameters.AddWithValue("@classID", classID);

                conn.Open();
                command.ExecuteNonQuery();
                MessageBox.Show("班级删除成功！");
                LogAction($"成功删除班级，对应id：{classID}");
                LoadClasses();
            }
        }

        // 更新班级
        private void UpdateClass(int classID, string newClassName, int newSchoolID)
        {
            using (var conn = db.GetConnection())
            {
                string query = "UPDATE Class SET ClassName = @className, SchoolID = @schoolID WHERE ClassID = @classID";
                MySqlCommand command = new MySqlCommand(query, conn);
                command.Parameters.AddWithValue("@className", newClassName);
                command.Parameters.AddWithValue("@classID", classID);
                command.Parameters.AddWithValue("@schoolID", newSchoolID);

                conn.Open();
                command.ExecuteNonQuery();
                MessageBox.Show("班级更新成功！");
                LogAction($"成功更新班级id为{classID}的信息：名字改为{newClassName}，学校对应id改为：{newSchoolID}");
                LoadClasses();
            }
        }

        // 记录用户操作到日志表
        private void LogAction(string action)
        {
            using (var conn = db.GetConnection())
            {
                string query = "INSERT INTO log (Action) VALUES (@action)";
                MySqlCommand command = new MySqlCommand(query, conn);
                command.Parameters.AddWithValue("@action", action);

                conn.Open();
                command.ExecuteNonQuery();
            }
        }

        // 按钮事件：添加班级
        private void btnAddClass_Click(object sender, EventArgs e)
        {
            string className = txtClassName.Text;
            int schoolID = int.Parse(txtSchoolID.Text);
            AddClass(className, schoolID);
        }

        // 按钮事件：删除班级
        private void btnDeleteClass_Click(object sender, EventArgs e)
        {
            int classID = int.Parse(txtClassID.Text);
            DeleteClass(classID);
        }

        // 按钮事件：更新班级
        private void btnUpdateClass_Click(object sender, EventArgs e)
        {
            int classID = int.Parse(txtClassID.Text);
            string newClassName = txtClassName.Text;
            int newSchoolID = int.Parse(txtSchoolID.Text);
            UpdateClass(classID, newClassName, newSchoolID);
        }
    }
}

